//
//  B_KeyboardTopView.m
//  testsdfsdfsdf
//
//  Created by 薛小刚 on 2018/9/12.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_KeyboardTopView.h"

@interface B_KeyboardTopView()<UITextViewDelegate>

@end

@implementation B_KeyboardTopView

- (instancetype)init {
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44);
        self.backgroundColor = [UIColor redColor];
        [self addSubViews];
        
//        [self addObserver:self forKeyPath:@"inputTextView.text" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}
- (void) addSubViews {
    [self addSubview:self.inputTextView];
    // 通过masonry 设置imputTextView的高度
    self.inputTextView.frame = self.bounds;
//    self.inputTextView.text = @"测试";
}

- (BaseTextView *)inputTextView {
    if (!_inputTextView) {
        _inputTextView = [[BaseTextView alloc] init];
        _inputTextView.delegate = self;
        _inputTextView.backgroundColor = [UIColor lightGrayColor];
        _inputTextView.font = [UIFont systemFontOfSize:15];
        _inputTextView.textContainerInset = UIEdgeInsetsMake(10,0, 0, 0);
    }
    return _inputTextView;
}


- (void)textViewDidChange:(UITextView *)textView {
    NSLog(@"%@", textView.text);
    NSString * string = textView.text;
    CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT);
    CGRect frame = [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: textView.font} context:nil];
    if (frame.size.height + 22 > 44) {
        self.frame = CGRectMake(0, -(frame.size.height + 22 - 44), [UIScreen mainScreen].bounds.size.width, frame.size.height + 22);
        self.inputTextView.frame = self.bounds;
    }
}

@end
